{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.session import GsSession, Environment\n",
    "from gs_quant.instrument import IRSwap\n",
    "from gs_quant.risk import IRFwdRate, RollFwd\n",
    "from gs_quant.markets.portfolio import Portfolio\n",
    "from gs_quant.markets import PricingContext\n",
    "from datetime import datetime\n",
    "import matplotlib.pylab as plt\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics', ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ccy = 'EUR'\n",
    "# construct a series of 6m FRAs going out 20y or so\n",
    "fras = Portfolio([IRSwap('Pay', '6m', ccy, effective_date='{}m'.format(i-6), \n",
    "                         fixed_rate_frequency='6m', floating_rate_frequency='6m') for i in range(6, 123, 6)])\n",
    "fras.resolve()\n",
    "results = fras.calc(IRFwdRate)\n",
    "\n",
    "# get the fwd rates for these fras under the base sceneraio (no shift in time)\n",
    "base = {fras[i].termination_date: res for i, res in enumerate(results)}\n",
    "base_series = pd.Series(base, name='base', dtype=np.dtype(float))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# calculate the fwd rates with a shift forward of 6m.  This shift keeps fwd rates constant.  \n",
    "# So 5.5y rate today will be 5y rate under the scenario of pricing 6m in the future.\n",
    "with RollFwd(date='6m', realise_fwd=True, holiday_calendar='LDN'):\n",
    "    results_fwd = fras.calc(IRFwdRate)\n",
    "with RollFwd(date='6m', realise_fwd=False, holiday_calendar='LDN'):\n",
    "    results_spot = fras.calc(IRFwdRate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "roll_to_fwds = {fras[i].termination_date: res for i, res in enumerate(results_fwd)}\n",
    "roll_to_fwds_series = pd.Series(roll_to_fwds, name='roll to fwd', dtype=np.dtype(float))\n",
    "\n",
    "roll_to_spot = {fras[i].termination_date: res for i, res in enumerate(results_spot)}\n",
    "roll_to_spot_series = pd.Series(roll_to_spot, name='roll to spot', dtype=np.dtype(float))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# show the curves, the base in blue, the roll to fwd in green and the roll to spot in orange.\n",
    "# note blue and green curves are not exactly on top of each other as we aren't using the curve instruments themselves\n",
    "# but instead using FRAs to show a smooth curve.\n",
    "base_series.plot(figsize=(20, 10))\n",
    "roll_to_spot_series.plot()\n",
    "roll_to_fwds_series.plot()\n",
    "plt.legend()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
